package com.example.mobile2021606134chhnewdemo.LoginModule;

import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.mobile2021606134chhnewdemo.MainActivity;
import com.example.mobile2021606134chhnewdemo.NewsModule.News;
import com.example.mobile2021606134chhnewdemo.NewsModule.NewsActivity;
import com.example.mobile2021606134chhnewdemo.R;

public class RegisterActivity01 extends AppCompatActivity {

    private LinearLayout UserNameLayout;
    private EditText UserNameEdit;
    private LinearLayout PassWordLayout;
    private EditText PassWordEdit;
    private LinearLayout PasswordAgainLayout;
    private EditText PassWordAgainEdit;
    private LinearLayout EmailLayout;
    private EditText EmailEdit;
    private LinearLayout ButtonLayout;
    private Button SignUpButton;
    private Button BackLoginButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_register01);
        initView();
        BackLoginButton.setOnClickListener(v -> {
            Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
            startActivity(intent);
            finish();
        });
        SignUpButton.setOnClickListener(v -> {
            String strUserName = UserNameEdit.getText().toString().trim();
            String strPassWord = PassWordEdit.getText().toString().trim();
            String strPassWordAgain = PassWordAgainEdit.getText().toString().trim();
            String strPhoneNumber = EmailEdit.getText().toString().trim();
            //注册格式粗检
            if (strUserName.length() > 10) {
                Toast.makeText(RegisterActivity01.this, "用户名长度必须小于10！", Toast.LENGTH_SHORT).show();
            } else if (strUserName.length() < 4) {
                Toast.makeText(RegisterActivity01.this, "用户名长度必须大于4！", Toast.LENGTH_SHORT).show();
            } else if (strPassWord.length() > 16) {
                Toast.makeText(RegisterActivity01.this, "密码长度必须小于16！", Toast.LENGTH_SHORT).show();
            } else if (strPassWord.length() < 6) {
                Toast.makeText(RegisterActivity01.this, "密码长度必须大于6！", Toast.LENGTH_SHORT).show();
            } else if (!strPassWord.equals(strPassWordAgain)) {
                Toast.makeText(RegisterActivity01.this, "两次密码输入不一致！", Toast.LENGTH_SHORT).show();
            } else if (!strPhoneNumber.contains("@")) {
                Toast.makeText(RegisterActivity01.this, "邮箱格式不正确！", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(RegisterActivity01.this, "注册成功，自动登录中！", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(RegisterActivity01.this, NewsActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initView() {
        UserNameLayout = (LinearLayout) findViewById(R.id.UserNameLayout);
        UserNameEdit = (EditText) findViewById(R.id.UserNameEdit);
        PassWordLayout = (LinearLayout) findViewById(R.id.PassWordLayout);
        PassWordEdit = (EditText) findViewById(R.id.PassWordEdit);
        PasswordAgainLayout = (LinearLayout) findViewById(R.id.PasswordAgainLayout);
        PassWordAgainEdit = (EditText) findViewById(R.id.PassWordAgainEdit);
        EmailLayout = (LinearLayout) findViewById(R.id.EmailLayout);
        EmailEdit = (EditText) findViewById(R.id.EmailEdit);
        ButtonLayout = (LinearLayout) findViewById(R.id.ButtonLayout);
        SignUpButton = (Button) findViewById(R.id.SignUpButton);
        BackLoginButton = (Button) findViewById(R.id.BackLoginButton);
    }
}